大数据基础编程、实验和教程案例(实验四)
14.4 实验四:NoSQL 和关系数据库的操作比较
本实验对应第 6 章的内容。
14.4.1 实验目的
(1)理解四种数据库(MySQL、HBase、Redis 和 MongoDB)的概念以及不同点; (2)熟练使用四种数据库操作常用的 Shell 命令; (3)熟悉四种数据库操作常用的 Java API。
14.4.2 实验平台
操作系统LinuxHadoop版本3.1.3MySQL 版本5.6HBase 版本2.2.2Redis 版本5.0.5MongoDB 版本4.2.0JDK 版本1.8Java IDEEclipse
14.4.3 实验步骤
(一) MySQL 数据库操作
根据上面给出的 Student 表,在 MySQL 数据库中完成如下操作: (1)在 MySQL 中创建 Student 表,并录入数据 (2)用 SQL 语句输出 Student 表中的所有记录 (3)查询 zhangsan 的 Computer 成绩 (4)修改 lisi 的 Math 成绩,改为 95 2.根据上面已经设计出的 Student 表,使用 MySQL 的 JAVA 客户端编程实现以下操作: (1)向 Student 表中添加如下所示的一条记录: scofield 45 89 100
import java.sql.*;
public class mysql_test {
/**
* @param args
*/
//JDBC DRIVER and DB
static final String DRIVER="com.mysql.jdbc.Driver";
static final String DB="jdbc:mysql://localhost/mysql";
//Database auth
static final String USER="root";
static final String PASSWD="hadoop";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
try {
//加载驱动程序
Class.forName(DRIVER);
System.out.println("Connecting to a selected database...");
//打开一个连接
conn=DriverManager.getConnection(DB, USER, PASSWD);
//执行一个查询
stmt=conn.createStatement();
String sql="insert into student values('scofield',45,89,100)";
stmt.executeUpdate(sql);
System.out.println("Inserting records into the table successfully!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(2)获取 scofield 的 English 成绩信息
import java.sql.*;
public class mysql_qurty {
/**
* @param args
*/
//JDBC DRIVER and DB
static final String DRIVER="com.mysql.jdbc.Driver";
static final String DB="jdbc:mysql://localhost/mysql";
//Database auth
static final String USER="root";
static final String PASSWD="hadoop";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
//加载驱动程序
Class.forName(DRIVER);
System.out.println("Connecting to a selected database...");
//打开一个连接
conn=DriverManager.getConnection(DB, USER, PASSWD);
//执行一个查询
stmt=conn.createStatement();
String sql="select name,English from student where name='scofield' ";
//获得结果集
rs=stmt.executeQuery(sql);
System.out.println("name"+"\t\t"+"English");
while(rs.next())
{
System.out.print(rs.getString(1)+"\t\t");
System.out.println(rs.getInt(2));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(rs!=null)
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/00a6c4b51c6944b48acd5d9194c88340.png)
(二) HBase 数据库操作
根据上面给出的学生表 Student 的信息,执行如下操作: (1) 用 Hbase Shell 命令创建学生表 Student (2)用 scan 指令浏览 Student 表的相关信息 (3)查询 zhangsan 的 Computer 成绩 (4)修改 lisi 的 Math 成绩,改为 95 hbase爆炸,做不了
(三)Redis 数据库操作
根据上面给出的键值对,完成如下操作: (1)用 Redis 的哈希结构设计出学生表 Student(键值可以用 student.zhangsan 和student.lisi 来表示两个键值属于同一个表); 2.根据上面已经设计出的学生表 Student,用Redis 的JAVA 客户端编程(jedis),实现如下操作: (1)添加数据:English:45 Math:89 Computer:100 scofield:{ English: 45 Math: 89 Computer: 100 } 完成添加数据操作的 Java 代码如下:
import java.util.Map;
import redis.clients.jedis.Jedis;
public class jedis_test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Jedis jedis = new Jedis("localhost");
jedis.hset("student.scofield", "English","45");
jedis.hset("student.scofield", "Math","89");
jedis.hset("student.scofield", "Computer","100");
Map value = jedis.hgetAll("student.scofield");
for(Map.Entry entry:value.entrySet())
{
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
(2)获取 scofield 的 English 成绩信息 获取 scofield 的 English 成绩信息的 Java 代码如下:
import java.util.Map;
import redis.clients.jedis.Jedis;
public class jedis_query {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Jedis jedis = new Jedis("localhost");
String value=jedis.hget("student.scofield", "English");
System.out.println("scofield's English score is: "+value);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/532adde195da493c8cbba4891d4bc6a4.png)
(四)MongoDB 数据库操作
1.根据上面给出的文档,完成如下操作: (1)用 MongoDB Shell 设计出 student 集合; (2)用 find()方法输出两个学生的信息; (3)用 find 函数查询 zhangsan 的所有成绩(只显示 score 列); (4)修改 lisi 的 Math 成绩,改为 95。 2.根据上面已经设计出的 Student 集合,用 MongoDB 的 Java 客户端编程,实现如下操作: (1)添加数据:English:45 Math:89 Computer:100
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class mongo_insert {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个mongo客户端
MongoClient mongoClient=new MongoClient("localhost",27017);
//实例化一个mongo数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("student");
//获取数据库中某个集合
MongoCollection collection = mongoDatabase.getCollection("student");
//实例化一个文档,内嵌一个子文档
Document document=new Document("name","scofield").
append("score", new Document("English",45).
append("Math", 89).
append("Computer", 100));
List documents = new ArrayList();
documents.add(document);
//将文档插入集合中
collection.insertMany(documents);
System.out.println("文档插入成功");
}
插入成功 (2)获取 scofield 的所有成绩成绩信息(只显示 score 列)
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import static com.mongodb.client.model.Filters.eq;
public class mongo_query {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化一个mongo客户端
MongoClient mongoClient=new MongoClient("localhost",27017);
//实例化一个mongo数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("student");
//获取数据库中某个集合
MongoCollection collection = mongoDatabase.getCollection("student");
//进行数据查找,查询条件为name=scofield, 对获取的结果集只显示score这个域
MongoCursor cursor=collection.find( new Document("name","scofield")).
projection(new Document("score",1).append("_id", 0)).iterator();
while(cursor.hasNext())
System.out.println(cursor.next().toJson());
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/c9310faa67bd4243bd8129d732a699a3.png)
|